﻿using System;
using System.Collections.Generic;
using Weight.Dal.CardManager;
using Weight.Model;
using Weight.Model.ParamEntity;
using Weight.Model.ResultEntity;

namespace Weight.Bll.CardManage
{
    /// <summary>
    /// 卡号管理业务类
    /// </summary>
    public class BllCardManage
    {
       
        private CardManagerDal dal = new CardManagerDal();

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
       public  CardManagerQueryRes List(QueryCardManagerReq req)
        {
            if (req == null) return new CardManagerQueryRes() { CardManagerList = new List<CardManagerEntity>()};
            if (req.PageSize == 0) req.PageSize = 30;
            if (req.PageIndex < 1) req.PageIndex = 1;
            return  dal.List(req);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="req">车号</param>
        /// <returns></returns>
        public IEnumerable<CardManagerEntity> Query(string plateNum)
        {
            return dal.Query(plateNum);
        }

        /// <summary>
        /// 根据Id查找记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CardManagerEntity GetById(string id)
        {
            if(string.IsNullOrEmpty(id))
            {
                return null;
            }
            return dal.GetById(id);
        }

        /// <summary>
        /// 根据卡号获取发卡数据
        /// </summary>
        /// <param name="rfNo">卡号</param>
        /// <returns></returns>
        public CardManagerEntity  GetCardByRfNo(string rfNo)
        {
            if(string.IsNullOrEmpty(rfNo))
            {
                return null;
            }
            else
            {
                return dal.GetCardByRfNo(rfNo);
            }
        }

        /// <summary>
        /// 获取未完成过磅的发卡数据
        /// </summary>
        /// <param name="rfNo">卡号</param>
        /// <returns></returns>
        public CardManagerEntity GetValidCardByRfNo(string rfNo)
        {
            if (string.IsNullOrEmpty(rfNo))
            {
                return null;
            }
            else
            {
                return dal.GetValidCardByRfNo(rfNo);
            }
        }

        /// <summary>
        /// 获取未完成过磅的发卡数据
        /// </summary>
        /// <param name="rfNo">卡号</param>
        /// <returns></returns>
        public CardManagerEntity GetValidCardByPlateNum(string plateNum)
        {
            if (string.IsNullOrEmpty(plateNum))
            {
                return null;
            }
            else
            {
                return dal.GetValidCardByPlateNum(plateNum);
            }
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Tuple<bool,string> Insert(CardManagerEntity entity)
        {
            if (entity == null) return new Tuple<bool, string>(false,"参数错误");
            bool b = dal.Insert(entity);
            if(b)
            {
                return new Tuple<bool, string>(true,"添加成功");
            }
            else
            {
                return new Tuple<bool, string>(false,"添加失败");
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Tuple<bool,string> Update(CardManagerEntity entity)
        {
            if (entity == null) return new Tuple<bool, string>(false,"参数错误");
            bool b = dal.Update(entity);
            if(b)
            {
                return new Tuple<bool, string>(true,"修改成功");
            }
            else
            {
                return new Tuple<bool, string>(false, "修改失败");
            }
        }

        /// <summary>
        /// 有效卡号记录是否已经存在
        /// </summary>
        /// <param name="rFNo">卡号</param>
        /// <returns></returns>
        public bool IsExistCard(string rFNo)
        {
            return dal.IsExistCard(rFNo);
        }

        /// <summary>
        /// 有效车号记录是否已经存在
        /// </summary>
        /// <param name="plateNum">车号</param>
        /// <returns></returns>
        public bool IsExistPlateNum(string plateNum)
        {
            return dal.IsExistPlateNum(plateNum);
        }

        /// <summary>
        /// 有效车号(不包括指定卡号)是否已经存在
        /// </summary>
        /// <param name="rFNo">卡号</param>
        /// <param name="platenum">车号</param>
        /// <returns></returns>
        public bool IsExistPlatenumExceptRFNo(string rFNo, string platenum)
        {
            return dal.IsExistPlatenumExceptRFNo(rFNo,platenum);
        }

        /// <summary>
        /// 有效卡号(不包括指定车号)是否已经存在
        /// </summary>
        /// <param name="rFNo">卡号</param>
        /// <param name="platenum">车号</param>
        /// <returns></returns>
        public bool IsExistRFNoExceptPlatenum(string rFNo, string platenum)
        {
            return dal.IsExistRFNoExceptPlatenum(rFNo, platenum);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(string id)
        {
            return dal.Del(id);
        }

        /// <summary>
        /// 补卡
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="newRfNo">新的卡号</param>
        /// <returns></returns>
        public bool SupplyCardNo(string id, string newRfNo)
        {
            if(string.IsNullOrEmpty(id) || string.IsNullOrEmpty(newRfNo))
            {
                return false;
            }
            else
            {
                return dal.SupplyCardNo(id,newRfNo);
            }
        }

        /// <summary>
        /// 销卡
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public Tuple<bool,string> UnbindCard(string id)
        {
            if(string.IsNullOrEmpty(id))
            {
                return new Tuple<bool, string>(false,"编号不能为空") ;
            }
            else
            {
                bool b=  dal.UnbindCard(id);
                if(b)
                {
                    return new Tuple<bool, string>(true,"销卡成功");
                }
                else
                {
                    return new Tuple<bool, string>(false,"销卡失败");
                }
            }
        }

        /// <summary>
        /// 获取待过磅的卡数量
        /// </summary>
        /// <returns></returns>
        public int GetRedeayCardCount()
        {
            return dal.GetRedeayCardCount();
        }
    }
}
